package com.foxit.setting;

import java.io.*;
import java.net.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.text.SimpleDateFormat;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class SetMember extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
       request.setCharacterEncoding("UTF-8");
       PrintWriter out = response.getWriter();
       try {
          MongoClient mongo = new MongoClient("192.201.108.97", 27017);
          DB db = mongo.getDB("settingdb");
          DBCollection table = db.getCollection("member");
    //      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
     //     sdf.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
          String r = request.getParameter("del");
          if(r != null && r.length()!=0) {
             String s=request.getParameter("member");
             table.remove(new BasicDBObject().append("start", r).append("member",Integer.valueOf(s)));
             response.sendRedirect("./SetMember?modify="+r);
             return;
          }
          String d = request.getParameter("modify");
          if(d == null || d.length()==0) {
             out.println("going to setting home");
             response.sendRedirect("./Sethome");
             return;
          }
          int member, type;
          String memo,memstr,types;
          int i=0;
          for(i=1;i <= 5;i++) {
             memstr = request.getParameter("member"+i); 
             if(memstr != null && memstr.length() != 0) {
                member = Integer.valueOf(memstr);
                type = Integer.valueOf(request.getParameter("type"+i));
                memo = request.getParameter("memo"+i);
                BasicDBObject doc = new BasicDBObject("start",d)
                   .append("member",member)
                   .append("type",type)
                   .append("memo",memo);
                table.insert(doc);
             }
          }

          out.println("<HTML><head><style>table,th,td { border:1px solid black;border-collapse:collapse; } </style></head><body><FORM action='./SetMember'><input type='hidden' name='modify' value='"+d+"'><table border='1' style='width:300px'><tr><th colspan=5>FOXIT Setting Tool(MEMBER)</th></tr>");
          out.println("<tr><th>member type</th><th>給廣告type</th><th>memo</th><th></th></tr>"); 
          BasicDBObject query = new BasicDBObject("start", d);
          DBCursor cursor = table.find(query);
          while (cursor.hasNext()) {
             Member s = (Member) JSON.parseObject(cursor.next().toString(),Member.class);
             out.println("<tr><td>"+s.getMember()+"</td><td>"+s.getType()+"</td><td>"+s.getMemo()+"</td><td><a href='./SetMember?del="+d+"&member="+s.getMember()+"'>刪除</a></td>");
          }
          cursor.close();
          for(i=1;i <= 5;i++) {
             out.println("<tr><td><input type='text' name='member"+i+"'></td>");
             out.println("<td><input type='text' name='type"+i+"'></td>");
             out.println("<td><input type='text' name='memo"+i+"'></td>");
             out.println("<td></td></tr>"); 
          }
          out.println("<td colspan=2><input type='submit' value='submit'></td><td colspan=2><a href='./Slist?modify="+d+"'>回setting主選單</a></td></tr></table></form></body></html>");
       //} catch (UnknownHostException e) {
       //} catch (MongoException e) {
       } catch (Exception ex) {
          out.println(ex.getMessage());
       }

    }
} 
